package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderEntityItem;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author: 伯俊软件
 * @description: 线上自提订单详情
 * @date: 2021/7/6 9:28
 * @param
 * @return
 */
public class FindOrderDetails extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public FindOrderDetails(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    public OrderEntityItem getData(String xsno) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(xsno), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        OrderEntityItem orderEntityItem = new OrderEntityItem();

        orderEntityItem.setId(jsonArray.getJSONArray(0).getString(0));
        orderEntityItem.setUserName(jsonArray.getJSONArray(0).getString(1));
        orderEntityItem.setSelfTime(jsonArray.getJSONArray(0).getString(2));
        orderEntityItem.setPayment(jsonArray.getJSONArray(0).getString(3));
        orderEntityItem.setXSNO(jsonArray.getJSONArray(0).getString(4));
        orderEntityItem.setSelfRasiNO(jsonArray.getJSONArray(0).getString(5));
        orderEntityItem.setCreateTime(jsonArray.getJSONArray(0).getString(6));
        orderEntityItem.setEndTime(jsonArray.getJSONArray(0).getString(7));
        orderEntityItem.setPhone(jsonArray.getJSONArray(0).getString(8));
        orderEntityItem.setOpenId(jsonArray.getJSONArray(0).getString(9));

        return orderEntityItem;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(String xsno) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'YC_RETAIL_ID;ID'");//ID
        columns.add("'YC_RETAIL_ID;C_VIP_ID;VIPNAME'");//顾客姓名
        columns.add("'YC_RETAIL_ID;SELFRAIS_DATE'");//自提时间
        columns.add("'YC_RETAIL_ID;TOT_AMT_ACTUAL'");//付款总额
        columns.add("'YC_RETAIL_ID;XSNO'");//线上单号
        columns.add("'YC_RETAIL_ID;SELFRAISNO'");//提货码
        columns.add("'YC_RETAIL_ID;STATUSTIME'");//下单时间
        columns.add("STATUSTIME");//提交时间
        columns.add("'YC_RETAIL_ID;RECEIVER_MOBILE'");//收货人手机号
        columns.add("'YC_RETAIL_ID;C_VIP_ID;OPENID'");//openid

        params.put("columns", columns);
        params.put("table", "YC_RETAILOUT");//表名

        HashMap paramss = new HashMap();

        HashMap expr1 = new HashMap();
        expr1.put("column","'YC_RETAIL_ID;STATUS'");
        expr1.put("condition","2");

        HashMap expr2 = new HashMap();
        expr2.put("column","'YC_RETAIL_ID;XSNO'");
        expr2.put("condition","'"+xsno+"'");


        paramss.put("combine","and");
        paramss.put("expr1",expr1);
        paramss.put("expr2",expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);

    }

    public static void main(String[] args) {
        FindOrderDetails g = new FindOrderDetails("39.104.240.134:55321", "nea@burgeon.com.cn", "root55321");
        try {
            System.err.println(g.getData("1000000032134010"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
